//
//  GamePlayLayer.cpp
//  MathSpeed
//
//  Created by ToanTM on 5/16/14.
//
//

#include "GameMenuLayer.h"
#include "GameConfig.h"
#include "SimpleAudioEngine.h"
#include "NativeHelper.h"

bool GameMenuLayer::mIsShowSplashScreen = true;

CCScene* GameMenuLayer::scene(){
    CCScene* scene = CCScene::create();
    GameMenuLayer* layer = GameMenuLayer::create();
    scene->addChild(layer);
    return scene;
}

bool GameMenuLayer::init(){
    if (!CCLayer::init()) {
        return false;
    }
    this->setKeypadEnabled(true);
    
    CCSprite * mainBackground = CCSprite::create(IMAGE_BACKGROUND_MAIN);
    mainBackground->setAnchorPoint(ccp(0, 0));
    addChild(mainBackground);
    
    CCPoint mPosCenter = ccp(winSize.width/2, winSize.height/2);

    CCLabelTTF *lbHard = CCLabelTTF::create("Hard", FONT_BERLIN_SANS_REGULAR, 60);
    CCLabelTTF *lbNormal = CCLabelTTF::create("Normal", FONT_BERLIN_SANS_REGULAR, 60);
    CCLabelTTF *lbEasy = CCLabelTTF::create("Easy", FONT_BERLIN_SANS_REGULAR, 60);
    CCMenuItemLabel *menuHard = CCMenuItemLabel::create(lbHard, this, menu_selector(GameMenuLayer::buttonMenuHardCallback));
    CCMenuItemLabel *menuNormal = CCMenuItemLabel::create(lbNormal, this, menu_selector(GameMenuLayer::buttonMenuNormalCallback));
    CCMenuItemLabel *menuEasy = CCMenuItemLabel::create(lbEasy, this, menu_selector(GameMenuLayer::buttonMenuEasyCallback));
    
    
    mMenu = CCMenu::create(menuEasy, menuNormal, menuHard, NULL);
    mMenu->alignItemsVerticallyWithPadding(-10);
    mMenu->setPosition(ccp(mPosCenter.x, winSize.height - 450));
    addChild(mMenu);
    
    CCLabelTTF* labelMenu = CCLabelTTF::create("GAME MODE", FONT_BERLIN_SANS_BOLD, 65);
//    labelMenu->setColor(ccc3(216, 242, 113));
    labelMenu->setPosition(ccp(mPosCenter.x, winSize.height - 234));
    addChild(labelMenu);
    
    
#if (CC_TARGET_PLATFORM == CC_TARGET_OS_IPHONE)
    if (mIsShowSplashScreen) {
        mIsShowSplashScreen = false;
        appear();
    }
#else
    mIsBackPress = false;
    if (mIsShowSplashScreen) {
        mIsShowSplashScreen = false;
        CCSprite * bgSp = CCSprite::create(IMAGE_BACKGROUND_SPLASHSCREEN);
        bgSp->setAnchorPoint(ccp(0, 0));
        addChild(bgSp);
        CCAction* action = CCSequence::create(CCDelayTime::create(2), CCCallFuncN::create(this, callfuncN_selector(GameMenuLayer::removeSplashScreen)), NULL);
        bgSp->runAction(action);
    }
#endif
    
    // setCurrentScreen
    NativeHelper::getInstance()->setCurrentScreen(SCREEN_GAME_MENU);
    return true;
}

void GameMenuLayer::removeSplashScreen(CCObject* sender)
{
    if (mIsBackPress) {
        return;
    }
    ((CCSprite *)sender)->removeFromParentAndCleanup(true);
    appear();
}

void GameMenuLayer::appear(){
    setPosition(ccp(-winSize.width, 0));
    CCAction* action = CCMoveTo::create(TIME_ANIMATION_APPEAR, ccp(0, 0));
    runAction(action);
}

void GameMenuLayer::buttonMenuHardCallback(CCObject* sender){
    CCLOG("buttonMenuHardCallback");
    startGamePlayLayer(GameMode::HARD);
}
void GameMenuLayer::buttonMenuNormalCallback(CCObject* sender){
    CCLOG("buttonMenuNormalCallback");
    startGamePlayLayer(GameMode::NORMAL);
}
void GameMenuLayer::buttonMenuEasyCallback(CCObject* sender){
    CCLOG("buttonMenuEasyCallback");
    startGamePlayLayer(GameMode::EASY);
}

void GameMenuLayer::startGamePlayLayer(GameMode::GamePlayMode mode){
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(SOUND_TOUCH_EFFECTS);
    this->mGameMode = mode;
    CCAction *action = CCSequence::create(CCMoveTo::create(TIME_ANIMATION_APPEAR, ccp(winSize.width, 0)), CCCallFunc::create(this, callfunc_selector(GameMenuLayer::gotoGamePlayLayer)), NULL);
    runAction(action);
}

void GameMenuLayer::gotoGamePlayLayer()
{
    CCScene* scene = GamePlayLayer::scene(mGameMode);
    CCDirector::sharedDirector()->replaceScene(scene);
}

void GameMenuLayer::keyBackClicked(void)
{
    mIsBackPress = true;
    CCDirector::sharedDirector()->end();
}
